﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace AlbonWeb.Manager.OrderManager
{
    public partial class BuyRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = " where statusID!=1";
                //绑定布料状态 
                string cmdstr = "select * from ClothStatus";
                SqlHelper.BindDropDownList(ddlStatus, "statusName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                SqlHelper.BindDropDownList(ddlMoveTo, "statusName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlMoveTo.Items.Insert(0, new ListItem("--select--","0"));
                ddlStatus.Items.Insert(0, new ListItem("--select--","0"));
                //绑定布料公司
                BLL.ClothCompanyBLL clothcompanybll = new BLL.ClothCompanyBLL();
                ddlCompany.DataSource = clothcompanybll.getActiveCompany();
                ddlCompany.DataTextField = "shortName";
                ddlCompany.DataValueField = "ID";
                ddlCompany.DataBind();
                ddlCompany.Items.Insert(0,new ListItem("--select--","0"));
                bindCloth();
            }
        }
        //绑定布料信息
        protected void bindCloth()
        {
            if (ddlStatus.SelectedIndex != 0)
            {
                ViewState["limit"] += " and statusID=" + ddlStatus.SelectedValue;
            }
            if (ddlCompany.SelectedIndex != 0)
            {
                ViewState["limit"] += " and clothCompany='" + ddlCompany.SelectedItem.Text + "'";
            }
            if (orderRef.Text.Trim() != "")
            {
                ViewState["limit"] += " and orderRefNo like '%"+orderRef.Text.Trim()+"%'";
            }
            if (clothRef.Text.Trim() != "")
            {
                ViewState["limit"] += " and clothRefNo like '%"+clothRef.Text.Trim()+"%'";
            }
            string cmdstr = "select count(*) from ClothBuyView " + ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","ClothBuyView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate desc")
            };
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["limit"] = " where statusID!=1";
            bindCloth();
        }

        //修改布料状态 
        protected void btnMove_Click(object sender, EventArgs e)
        {
            if (ddlMoveTo.SelectedIndex != 0)
            {
                string idlist = hfchecklist.Value;
                BLL.ClothBuyBLL clothbuybll = new BLL.ClothBuyBLL();
                clothbuybll.updateStatus(idlist, ddlMoveTo.SelectedValue, ddlMoveTo.SelectedItem.Text, User.Identity.Name);
                ddlMoveTo.SelectedIndex = 0;
                bindCloth();
            }
        }
        //换页 
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindCloth();
        }
    }
}